from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_socketio import Namespace
import eventlet

app = Flask(__name__, template_folder="templates")

app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, engineio_logger=True, logger=True, cors_allowed_origins='http://localhost:3000')


# class-based Namespaces self.emit()代替了 修饰器namespaces 的sio.emit()
class MyCustomNamespace(Namespace):

    def on_connect(self):
        print("Client just connected")
        return

    def on_disconnect(self):
        print("Client just left")

    def on_myWechat(self, *data):
        print('************')
        print(data)
        print(data[0])
        print(data[0][0])
        print(data[0][1])
        pass


# 注意:Flask-Socketio用on_namespace;Python-Socketio用register_namespace
socketio.on_namespace(MyCustomNamespace('/channel_A'))

# 启动方式2：
if __name__ == '__main__':
    SERVER_ADDRESS = ('127.0.0.1', 8000)
    sock = eventlet.listen(SERVER_ADDRESS)
    eventlet.wsgi.server(sock, app)
